//
//  SegmentView.m
//  MyMovie
//
//  Created by Weiwei on 16/3/2.
//  Copyright © 2016年 bever贝沃汇力 weiwei. All rights reserved.
//

#import "SegmentView.h"
@interface SegmentView(){
    
    NSInteger _itemsCount;//按钮组按钮的数量
    NSArray* _titlesArray;//存放BUTTON的title
    
    float _itemWidth;
    UIImageView* _selectImage;
    
}

@end

@implementation SegmentView

//一个view上放置多个button还有个selectImage
//自定义初始化方法，通过传入一个frame确定控件大小，闯入一个数组用来确定每一个button的title和button的数量

-(id)initWithFrame:(CGRect)frame withTitleArray:(NSArray*)array{
    
    //1.保留父类的方法
    if (self = [super initWithFrame:frame]) {
        
        //2.通过array确定Button 的数量和每一个的title
        _itemsCount = array.count;
        _titlesArray = array;
        //3.创建按钮的方法
        [self createSegment];
        
    }
    
    
    return self;
    
}
-(void)createSegment{
    
#pragma -mark 1-------循环创建按钮----------
    
    //0.设置按钮的宽
    _itemWidth = self.frame.size.width/_itemsCount;
    
    //1.for循环创建按钮
    for (int i = 0; i< _itemsCount; i++) {
        
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        //设置button的尺寸
        button.frame = CGRectMake(i*_itemWidth , 0, _itemWidth, self.frame.size.height);
        //设置button的title
        [button setTitle:_titlesArray[i] forState:UIControlStateNormal];
        //设置正常状态下的字体大小
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        //据中显示
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        //设置颜色
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        //tag值用于区分
        button.tag = 1000+i;
        
        [button addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
#pragma -mark 3-------创建的时候判断，如果是第一个，就特殊处理
        if (i == 0) {
            button.selected = YES;
            button.titleLabel.font = [UIFont systemFontOfSize:30];
        }
        [self addSubview:button];
    }
    
    
#pragma -mark 4-----------优化增加一个selectImage-----
    
    
    _selectImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-3, _itemWidth, 3)];
    _selectImage.image = [UIImage imageNamed:@"color_line"];
    
    [self addSubview:_selectImage];
    
    

    
}

-(void)selectAction:(UIButton*)button{
    
#pragma -mark 2------点击button修改系统的状态
    
    //(1)将所有的button都设置成select = no
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            
            UIButton* subButton  = (UIButton*)subView;
            subButton.selected = NO;
            //字体还原到20
            subButton.titleLabel.font = [UIFont systemFontOfSize:20];
            
        }
    }
    //(2)特殊化当前的button,设置成select ,字体变大25
    button.selected = YES;
    button.titleLabel.font = [UIFont systemFontOfSize:30];
    
#pragma -mark 5--------通过当前button的tag重新设置selectImage的位置
    //1.获得当前的button的tag，得到位置的index
    NSInteger index = button.tag -1000;
    [UIView animateWithDuration:.3 animations:^{
          _selectImage.frame = CGRectMake(index* _itemWidth, _selectImage.frame.origin.y, _selectImage.frame.size.width, _selectImage.frame.size.height);
    }];
    
    
#pragma -mark block回调
    _segmentBlock(index);
    
    
    
}

//-(void)chuanzhi:(SegmentBlock)segmentBlock{
//    
//    
//    _segmentBlock = segmentBlock;
//    
//}

@end
